<template>
  <view class="content">
    <cu-custom bgColor="bg-blue" :isBack="false">
    </cu-custom>
    <view style="text-align: center;">
      <image class="logo" :src="getAvatar()"></image>
      <view style="margin-bottom: 10px;">
        <span style="font-size:20px;">{{ settings['login.site_name'] || '后台管理系统' }}</span>
      </view>
      <view style="margin-bottom: 10px;">
        <span style="font-size:16px">欢迎使用</span>
      </view>
    </view>
    <view v-if="!userInfo || !userInfo.name" style="text-align: center;">
      <text class="title">请进行登录！</text>
    </view>
    <view v-else>
      <uni-section type="line" title="我的信息">
        <uni-list>
          <uni-list-item title="姓名" :rightText="userInfo.name"/>
          <uni-list-item title="手机号" :rightText="userInfo.mobile"/>
          <uni-list-item title="部门名称" :rightText="userInfo.dept_info && userInfo.dept_info.dept_name"/>
          <uni-list-item title="角色"
                         :rightText="userInfo.role_info && userInfo.role_info[0] &&userInfo.role_info[0].name"/>
        </uni-list>
      </uni-section>
      <view style="margin: 20px;margin-top: 40px;">
        <!--        <button type="primary" style="border-radius: 30px">扫码登录</button>-->
        <br>
        <button type="default" style="border-radius: 30px" @click="logout">退出登录</button>
      </view>
    </view>
  </view>
</template>

<script>
import {
  API_URL
} from "@/env";

export default {
  data() {
    return {
      title: 'Hello',
      loginUid: '',
      analysisData: {},
      loginData: {},
      userInfo: {},
      token: '',
      settings: uni.getStorageSync('third_settings') || {},
      isLoad: false,
      optionsToken: ''
    }
  },
  async onShow() {
    if (this.isLoad) {
      await this.initData()
    }
  },
  async onLoad(options) {
    console.log("options", options)
    this.loginUid = options.login_uid || ''
    this.optionsToken = options.token || ''
    if (this.optionsToken) {
      uni.setStorageSync('token', this.optionsToken)
    }
    await this.initData()
    this.isLoad = true
  },
  methods: {
    async initData() {
      if (this.optionsToken) {
        uni.setStorageSync('is_webview', true)
      }
      // 1. 校验是否有token
      this.token = uni.getStorageSync('token') || '';
      console.log('this.token', this.token)
      if (!this.token) {
        uni.redirectTo({
          url: this.loginUid ? ('/pages/login/index?login_uid=' + this.loginUid) : '/pages/login/index'
        });
        return
      }
      // 3. 获取用户信息
      await this.getUserInfo()
      this.getSettingsInfo()
      // 2. 校验是否有login_uid
      if (this.loginUid) {
        uni.redirectTo({
          url: '/pages/scanLogin/index?login_uid=' + this.loginUid,
        });
      }
    },
    getSettingsInfo() {
      uni.request({
        url: API_URL + 'api/init/settings/', //仅为示例，并非真实接口地址。
        success: (res) => {
          if (res.data.code === 2000 && res.data.data) {
            this.settings = res.data.data
            uni.setStorageSync('third_settings', this.settings)
          }
        }
      });
    },
    async getUserInfo() {
      return new Promise((resolve, reject) => {
        uni.request({
          url: API_URL + 'api/system/user/user_info/', //仅为示例，并非真实接口地址。
          header: {
            'Authorization': 'JWT ' + this.token
          },
          success: (res) => {

            if (res.data.code === 2000 && res.data.data) {
              this.userInfo = res.data.data
              resolve(res.data);
            } else {
              uni.showToast({
                title: '登录失效，重新登陆！',
                icon: 'error'
              })
              uni.setStorageSync('token', '');
              uni.redirectTo({
                url: '/pages/login/index',
              })
              reject(res);
            }
          },
          fail: (err) => {
            uni.showToast({
              title: '请求失败:' + String(err),
              icon: 'none'
            })
            reject(err);
          }
        });
      })
    },
    // 获取头像
    getAvatar() {
      if (this.userInfo && this.userInfo.avatar) {
        return API_URL + this.userInfo.avatar
      }
      return '/static/logo.png'
    },
    logout() {
      uni.setStorageSync('token', '');
      uni.redirectTo({
        url: '/pages/login/index',
      })
    }
  }
}
</script>

<style>
.content {
  /* display: flex; */
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 150rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}

button::after {
  border: none;
}


</style>
